package com.fan.shop.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.fan.shop.domain.ShopGlobal;

@Named("shopGlobalDao")
public class ShopGlobalDaoImpl implements ShopGlobalDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ShopGlobal getShopGlobal(String name) {
		Session session = sessionFactory.getCurrentSession();
		ShopGlobal shopGlobal = (ShopGlobal) session
				.get(ShopGlobal.class, name);
		return shopGlobal;
	}

	public List<ShopGlobal> getAllShopGlobals() {
		Session session = sessionFactory.getCurrentSession();
		List<ShopGlobal> list = session.createCriteria(ShopGlobal.class).list();
		return list;
	}

	public int getSize() {
		return getAllShopGlobals().size();
	}

	public void addShopGlobal(ShopGlobal shopGlobal) {
		Session session = sessionFactory.getCurrentSession();
		session.save(shopGlobal);
	}

	public void updateStock(ShopGlobal shopGlobal) {
		Session session = sessionFactory.getCurrentSession();
		session.update(shopGlobal);
	}

	public void deleteShopGlobal(ShopGlobal shopGlobal) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(shopGlobal);
	}

	public boolean hasShopGlobal(String name) {
		if (getShopGlobal(name) == null) {
			return false;
		} else {
			return true;
		}
	}

	public List<String> getColumnNames() {
		List<String> columnNames = new ArrayList<String>();
		columnNames.add("Name");
		columnNames.add("Description");
		columnNames.add("Value");
		return columnNames;
	}

}
